package com.scs.application.modules.sys.adapter;

import com.scs.application.modules.sys.properties.SystemProperties;
import com.scs.application.modules.sys.service.DictDataService;
import com.scs.application.modules.sys.service.DictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Description:数据字典适配器
 */
@Component
public class DictAdapter {
    @Autowired
    private SystemProperties systemProperties;

    private List<DictTypeService> dictTypeServices;

    private List<DictDataService> dictDataServices;

    @Autowired
    @Lazy
    private void setDictTypeServices(List<DictTypeService> dictTypeServices) {
        this.dictTypeServices = dictTypeServices;
    }

    @Autowired
    @Lazy
    private void setDictDataServices(List<DictDataService> dictDataServices) {
        this.dictDataServices = dictDataServices;
    }

    /**
     * 获取当前项目的DictType实现类
     * @return
     */
    public DictTypeService getDictTypeService() {
        DictTypeService dictTypeService = null;
        for(DictTypeService dictService : dictTypeServices) {
            if (dictService.isSupport(systemProperties.getFlagProject())) {
                dictTypeService = dictService;
                break;
            }
        }
        return dictTypeService;
    }

    /**
     * 获取当前项目的DictData实现类
     * @return
     */
    public DictDataService getDictDataService() {
        DictDataService dictDataService = null;
        for(DictDataService dictService : dictDataServices) {
            if (dictService.isSupport(systemProperties.getFlagProject())) {
                dictDataService = dictService;
                break;
            }
        }
        return dictDataService;
    }

    /**
     * 根据类型获取DictType实现类
     * @param flagProject
     * @return
     */
    public DictTypeService getDictTypeService(Boolean flagProject) {
        DictTypeService dictTypeService = null;
        for(DictTypeService dictService : dictTypeServices) {
            if (dictService.isSupport(flagProject)) {
                dictTypeService = dictService;
                break;
            }
        }
        return dictTypeService;
    }

    /**
     * 根据类型获取DictData实现类
     * @param flagProject
     * @return
     */
    public DictDataService getDictDataService(Boolean flagProject) {
        DictDataService dictDataService = null;
        for(DictDataService dictService : dictDataServices) {
            if (dictService.isSupport(flagProject)) {
                dictDataService = dictService;
                break;
            }
        }
        return dictDataService;
    }
}
